哇,第30天了,明天就是完賽心得囉~
歐吼吼~
role之前在playbook就有提過role
我們再來review一次
https://docs.ansible.com/ansible/latest/user_guide/playbooks_roles.html
https://github.com/ansible/ansible-examples/tree/master/wordpress-nginx
https://docs.ansible.com/ansible/latest/reference_appendices/galaxy.html#roles-path
預設role會下載到ANSIBLE-ROLES_PATH
$ ansible-galaxy install geerlingguy.apache -p roles
$ ansible-galaxy install git+https://github.com/geerlingguy/ansible-role-apache.git,0b7cd353c0250e87a26e0499e59e7fd265cc2f25
$ ansible-galaxy install -r requirements.yml
# from galaxy
- src: yatesr.timezone
# from GitHub
- src: https://github.com/bennojoy/nginx
# from GitHub, overriding the name and specifying a specific tag
- src: https://github.com/bennojoy/nginx
version: master
name: nginx_role
# from a webserver, where the role is packaged in a tar.gz
- src: https://some.webserver.example.com/files/master.tar.gz
name: http-role
# from Bitbucket
- src: git+https://bitbucket.org/willthames/git-ansible-galaxy
version: v1.4
# from Bitbucket, alternative syntax and caveats
- src: https://bitbucket.org/willthames/hg-ansible-galaxy
scm: hg
# from GitLab or other git-based scm, using git+ssh
- src: git@gitlab.company.com:mygroup/ansible-base.git
scm: git
version: "0.1" # quoted, so YAML doesn't parse this as a floating-point value
$ ansible-galaxy install geerlingguy.nginx -p roles
# 或者clone下來看看就好
$ git clone https://github.com/geerlingguy/ansible-role-nginx.git
# ROLE:nginx的目錄結構
roles/
ngix/ # 名字叫「nginx」的role
defaults/ # 預設變數,不管什麼OS,都一樣的變數
handlers/ # 就是你學的handler,統一整理在這邊
meta/ # role的依賴(dependencies)的其他role,
# https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html#role-default-variables
# 在插入其他role前,應該包含roles list跟參數
molecule/default # molecule是用來開發、測試Ansible role的
# https://molecule.readthedocs.io/en/latest/
tasks/ # 執行此 role 時的 main list of tasks,起點是tasks/main.yml
templates/ # 一些j2檔,例如config
# 有些 role 會有 files/ # 放一些檔案,例如事先下載好,要佈署到inventory的檔案
vars/ # 其他的變數,不同OS,各自都不同的變數
---
# Variable setup.
- name: Include OS-specific variables.
include_vars: "{{ ansible_os_family }}.yml"
# include
# https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_includes.html
# 應該也可以寫成這樣 {{ansible_facts['ansible_os_family']}}
# 像我的os是macOS,理論上應該要去include /vars/Debian.yml
# 註:沒有/vars/Ubuntu.yml
# 所以,我也不知道ubuntu的話,是不是就沒匯入,直接往下一步走@@~
- name: Define nginx_user.
set_fact:
nginx_user: "{{ __nginx_user }}"
when: nginx_user is not defined
# 在/vars/Debian.yml中 __nginx_user: "www-data"
# 應該是daemon的執行帳號吧
# 類似 const nginx_user = __nginx_user;
# Setup/install tasks.
- include_tasks: setup-Debian.yml
when: ansible_os_family == 'Debian'
# 會去跑setup-Debian.yml
# 我們直接來看setup-Debina.yml
# ---
# - name: Update apt cache.
# apt: update_cache=yes cache_valid_time=86400
# changed_when: false
# 類似下 apt-get update 指令
# - name: Ensure nginx is installed.
# apt:
# name: "{{ nginx_package_name }}"
# state: present
# default_release: "{{ nginx_default_release }}"
# 檢查有沒有裝nginx,沒裝就會裝apt-get install nginx
# Vhost configuration.
- import_tasks: vhosts.yml
# /tasks/vhosts.yml 有點長,就不列出來啦
# 主要會用到的東西有file、template、notify、tags
# 用來設定vhost.conf
# Nginx setup.
- name: Copy nginx configuration in place.
template:
src: "{{ nginx_conf_template }}" # 定義在/defaults/main.yml
dest: "{{ nginx_conf_file_path }}" # 定義在/vars/Debian.yml
owner: root
group: "{{ root_group }}"
mode: 0644
notify:
- reload nginx # 定義在 /handlers/main.yml
# 把/templates/nginx.conf.j2複製到/etc/nginx/nginx.conf
- name: Ensure nginx is started and enabled to start at boot.
service: name=nginx state=started enabled=yes
# day25有介紹的service
對於下載數、星星數這麼高的ROLE
如果你學完day25~day29
再依照ROLE的best practice的建議來理解各目錄
(defaults,handlers,meta,tasks,templates,vars)
可能會感覺這個ROLE是簡單的
我是用VS Code看的
遇到{{變數}},我會用VS Code的搜尋去找
1、唯一比較難的,個人認為是/tasks/vhosts.yml
{{ item.filename|default(item.server_name.split(' ')[0] ~ '.conf') }}
當item.filename is undefine時
則item.filename 的預設值為 item.server_name 第1個空白以前的字串 + '.conf'
~ 是字串連接的符號
這邊會覺得難,就是要再去學jinja2語法,關鍵字「jinja2語法」
另外提供2個簡單Jinja 2 template的split範例:
# 例用debug來練習split(),還蠻方便的
- hosts: all
vars:
ip: "192.168.14.21" # 假設有一個變數ip
tasks:
- debug:
msg: "{{ ip.split('.') }}" # 用.來split
output # play一下看結果
------
"msg": [
"192",
"168",
"14",
"21"
]
- hosts: all
vars:
test: ['192','168','14','21'] # 一個4個elements的array
tasks:
- debug:
msg: "{{ test | join('.') }}" # 用 join '.'串起來
output
------
ok: [localhost] => {
"msg": "192.168.14.21"
}
# 假設有一個變數hostname如下
# hostname = dev.example.com
# 用下面2種方式,可以得到一樣的結果
# 用.去split,每一個字串都join ',dc='
ldap_server = "dc={{ hostname | split('.') | join(',dc=') }}"
# 使用split_regex()
ldap_server = "dc={{ hostname | split_regex('\.') | join(',dc=') }}"
ldap_server = dc=dev,dc=example,dc=com
2、比較不懂的是如果os是ubuntu,沒有/vars/Ubuntu.yml這個檔案
至於Kyle Bai大大的kube-ansible就龐大多啦
https://github.com/kairen/kube-ansible
小弟覺得很值得花時間研究kube-ansible,也是從/tasks/main.yml慢慢看
也能充分了解k8s各元件的安裝、設定方式
但因為理解有限,就不打成文章啦
題外話…
其實偶還有google到一個看起來很強大、很完整的建k8s cluster的ansible專案
但可能會有資安問題,所以就不敢po上來
偶個人會clone下來「看」那個專案,學習ansible怎麼建k8s,但不會執行它
雖然ansible很方便,但你還是得瞭解它做了哪些事
正式環境建議自己重寫一個ansible專案,把看得懂、有把握的部分再納進來
這樣風險才能控制在 ansible本身的弱點 & 你用到的工具的弱點 以內